<?php

/**
 * Função que realiza o autoload das classes
 * Function to make the loading of the classes
 * @param string $className
 */
function __autoload($className){
	$baseDir = realpath(dirname(__FILE__) . '/../') . '/';
	if(stristr($className, '_model') !== false)
		$strFile = findFilesRecursive($baseDir . 'models/', str_ireplace('_model', '', $className) . '.php');
	else if(stristr($className, '_controller') !== false) {
		if(($strFile = findFilesRecursive($baseDir . 'sys/', strtolower(str_ireplace('_controller', '', $className)) . '.php')) === false)
			$strFile = findFilesRecursive($baseDir . 'modules/', strtolower(str_ireplace('_controller', '', $className)) . '.php');
	}
	else
		$strFile = findFilesRecursive($baseDir . 'helpers/', strtolower($className) . '.php');

	if($strFile !== false)
		include($strFile);
}

/**
 * Função que realiza a busca recursiva nos diretórios apontados pelo autoload
 * Function to realizes a recursive search on the directories used by the autoload function
 * @param $strPath
 * @param $strFileName
 * @return unknown_type
 */
function findFilesRecursive($strPath, $strFileName) {
	if(is_dir($strPath)) {
		$arrContent = scandir($strPath);
		foreach($arrContent as $strContent) {
			if($strContent != '.' && $strContent != '..' && is_dir($strPath . '/' . $strContent)) {
				if(($strRetorno = findFilesRecursive($strPath . '/' . $strContent, $strFileName)) !== false)
					return $strRetorno;
			}
			else if($strContent == $strFileName)
				return $strPath . '/' . $strFileName;
		}
	}
	
	return false;
}
?>